package Guangnan.Wu.clock.servlet;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LocaleServlet
 */
@WebServlet("/setLocale")
public class LocaleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	//explicitly setting the default locale
	//setting other Locales
	
    public LocaleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub 
		HttpSession session = request.getSession();
		session.invalidate();
		//ClockServlet cs= new ClockServlet();
		RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Locale locale = Locale.getDefault();
		if (request.getParameter("ca_en") != null && request.getParameter("ca_en").equals("Canada - English"))
		{
			locale = Locale.CANADA;
		}
		else if (request.getParameter("ca_fr") != null && request.getParameter("ca_fr").equals("Canada - French"))
		{
			locale = Locale.CANADA_FRENCH;
		}
		else if (request.getParameter("cn_zh") != null && request.getParameter("cn_zh").equals("China - Chinese"))
		{
			locale = Locale.CHINESE;
		}
		HttpSession session = request.getSession();
		session.setAttribute("locale", locale);
		
		//request.setAttribute("locale", locale);
		RequestDispatcher rd = request.getRequestDispatcher("clock");
		rd.forward(request, response);

	}

}
